interface EnvConfig {
  VITE_APP_TITLE: string;
  VITE_APP_API_BASE_URL: string;
  VITE_APP_PUBLIC_PATH: string;
  VITE_APP_UPLOAD_URL: string;
  VITE_APP_PORT: string;
  VITE_APP_MOCK: string;
  VITE_APP_ENV: string;
}

// 获取环境变量，带类型检查
export const getEnvValue = <K extends keyof EnvConfig>(key: K): string => {
  const envValue = import.meta.env[key];
  if (envValue === undefined) {
    throw new Error(`环境变量 ${key} 未定义`);
  }
  return envValue;
};

// 环境判断
export const isProd = import.meta.env.PROD;
export const isDev = import.meta.env.DEV;
export const isMock = getEnvValue('VITE_APP_MOCK') === 'true';

// 常用环境变量
export const ENV = {
  APP_TITLE: getEnvValue('VITE_APP_TITLE'),
  API_BASE_URL: getEnvValue('VITE_APP_API_BASE_URL'),
  PUBLIC_PATH: getEnvValue('VITE_APP_PUBLIC_PATH'),
  UPLOAD_URL: getEnvValue('VITE_APP_UPLOAD_URL'),
  PORT: parseInt(getEnvValue('VITE_APP_PORT'), 10),
  ENV_MODE: getEnvValue('VITE_APP_ENV'),
} as const;

// 环境配置
export const getApiBaseUrl = () => {
  if (isDev && isMock) {
    return '/mock-api';
  }
  return ENV.API_BASE_URL;
}; 